package com.mall.service.marketing.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.ssh.Sftp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.SmsCouponProductRelation;
import com.mall.model.domain.SmsFlashPromotionProductRelation;
import com.mall.model.domain.SmsFlashPromotionSession;
import com.mall.model.vo.WangHaoVO;
import com.mall.service.marketing.mapper.FlashProductRelationMapper;
import com.mall.service.marketing.mapper.FlashSessionMapper;
import com.mall.service.marketing.service.FlashSessionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FlashSessionServiceImpl
 * @Description
 * @Author Young
 * @Date 2023/3/12 12:35
 * @Version 1.0
 */
@Service
public class FlashSessionServiceImpl extends ServiceImpl<FlashSessionMapper,SmsFlashPromotionSession> implements FlashSessionService {

    @Autowired
    private FlashSessionMapper flashSessionMapper;
    @Autowired
    private FlashProductRelationMapper flashProductRelationMapper;

    /**
     * 添加场次
     * @param sfps
     */
    @Override
    public void create(SmsFlashPromotionSession sfps) {
        flashSessionMapper.insert(sfps);
    }

    /**
     * 删除场次
     * @param id
     */
    @Override
    public void delete(Integer id) {
        flashSessionMapper.deleteById(id);
    }

    /**
     * 获取全部可选场次及其数量
     * @param flashPromotionId
     * @return
     */
    @Override
    public List<WangHaoVO> selectList(Integer flashPromotionId) {
        List<WangHaoVO> list = new ArrayList<>();
        //先获取到活动联系表里可以使用的活动id
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);

        List<SmsFlashPromotionProductRelation> sfppr = flashProductRelationMapper.selectList(queryWrapper);

        List<Long> flashPromotionSessionId = CollUtil.getFieldValues(sfppr, "flashPromotionSessionId", Long.class);
        if (!CollUtil.isEmpty(flashPromotionSessionId)) {
            for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : sfppr) {
                WangHaoVO wangHaoVO = new WangHaoVO();
                SmsFlashPromotionSession smsFlashPromotionSession = flashSessionMapper.selectById(smsFlashPromotionProductRelation.getFlashPromotionSessionId());
                BeanUtils.copyProperties(smsFlashPromotionSession,wangHaoVO);
                wangHaoVO.setProductCount(smsFlashPromotionProductRelation.getFlashPromotionCount());
                list.add(wangHaoVO);
            }
            return list;
        }
        return null;
    }

    /**
     * 修改启用状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        SmsFlashPromotionSession sfps = new SmsFlashPromotionSession();
        sfps.setId(Long.valueOf(id));
        sfps.setStatus(status);
        flashSessionMapper.updateById(sfps);
    }

    /**
     * 修改场次
     * @param id
     * @param sfps
     */
    @Override
    public void updateId(Integer id, SmsFlashPromotionSession sfps) {
        flashSessionMapper.updateById(sfps);
    }

    /**
     * 获取场次详情
     * @param id
     */
    @Override
    public SmsFlashPromotionSession get(Integer id) {
        SmsFlashPromotionSession smsFlashPromotionSession = flashSessionMapper.selectById(id);
        return smsFlashPromotionSession;
    }
}
